package attend.Interceptor;

import static org.apache.struts2.StrutsStatics.HTTP_REQUEST;
import static org.apache.struts2.StrutsStatics.HTTP_RESPONSE;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.CookiesAware;

import attend.UserContainer.UserAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CookieInterceptor extends AbstractInterceptor {

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
	 */
	public String intercept(ActionInvocation invocation) throws Exception
	{
		ActionContext 		context 	= 	invocation.getInvocationContext();
		HttpServletRequest  request 	= 	( HttpServletRequest ) context.get(HTTP_REQUEST);
		HttpServletResponse  response 	= 	( HttpServletResponse ) context.get(HTTP_RESPONSE);
		
		Cookie[] cookie	=	request.getCookies();
		
		for (Cookie cookie2 : cookie) {
			System.out.println(cookie2.getDomain());
			System.out.println(cookie2.getMaxAge());
			System.out.println(cookie2.getName());
			System.out.println(cookie2.getPath());
			System.out.println(cookie2.getValue());
		}
		
		Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
		
		for (Cookie ck : cookie) {
			cookieMap.put("cookie", ck);
		}
		
		Object  action = invocation.getAction();
		
		if(action instanceof UserAware){
			((CookiesAware)action).setCookiesMap(cookieMap);
		}
		
		return invocation.invoke();
	}
}
